﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;

namespace GtsServer.DAL
{
    /// <summary>
    /// 数据访问类:PrizeRecord
    /// </summary>
    public partial class PrizeRecordDAL
    {
        public PrizeRecordDAL()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from PrizeRecord");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.PrizeRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PrizeRecord(");
            strSql.Append("UserID,PrizeID,CreateTime,EditTime,State,OrderAccount,UserName,UserQQ,UserPhone,UserAddr)");
            strSql.Append(" values (");
            strSql.Append("@UserID,@PrizeID,@CreateTime,@EditTime,@State,@OrderAccount,@UserName,@UserQQ,@UserPhone,@UserAddr)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@PrizeID", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@OrderAccount", SqlDbType.VarChar,50),
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@UserQQ", SqlDbType.VarChar,20),
					new SqlParameter("@UserPhone", SqlDbType.VarChar,20),
					new SqlParameter("@UserAddr", SqlDbType.VarChar,100)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.PrizeID;
            parameters[2].Value = model.CreateTime;
            parameters[3].Value = model.EditTime;
            parameters[4].Value = model.State;
            parameters[5].Value = model.OrderAccount;
            parameters[6].Value = model.UserName;
            parameters[7].Value = model.UserQQ;
            parameters[8].Value = model.UserPhone;
            parameters[9].Value = model.UserAddr;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.PrizeRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PrizeRecord set ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("PrizeID=@PrizeID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("State=@State,");
            strSql.Append("OrderAccount=@OrderAccount,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("UserQQ=@UserQQ,");
            strSql.Append("UserPhone=@UserPhone,");
            strSql.Append("UserAddr=@UserAddr");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@PrizeID", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@OrderAccount", SqlDbType.VarChar,50),
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@UserQQ", SqlDbType.VarChar,20),
					new SqlParameter("@UserPhone", SqlDbType.VarChar,20),
					new SqlParameter("@UserAddr", SqlDbType.VarChar,100),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.PrizeID;
            parameters[2].Value = model.CreateTime;
            parameters[3].Value = model.EditTime;
            parameters[4].Value = model.State;
            parameters[5].Value = model.OrderAccount;
            parameters[6].Value = model.UserName;
            parameters[7].Value = model.UserQQ;
            parameters[8].Value = model.UserPhone;
            parameters[9].Value = model.UserAddr;
            parameters[10].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateState(GtsServer.Model.PrizeRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PrizeRecord set ");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("State=@State");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.EditTime;
            parameters[1].Value = model.State;
            parameters[2].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PrizeRecord ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PrizeRecord ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }




        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GtsServer.Model.PrizeRecordShow GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 pr.ID,pr.UserID,pr.PrizeID,pr.CreateTime,pr.EditTime,pr.State,pr.OrderAccount,pr.UserName,pr.UserQQ,pr.UserPhone,pr.UserAddr,p.Name PrizeName from PrizeRecord pr left join Prize p on pr.PrizeID=p.ID ");
            strSql.Append(" where pr.ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            GtsServer.Model.PrizeRecordShow model = new GtsServer.Model.PrizeRecordShow();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserID"] != null && ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PrizeID"] != null && ds.Tables[0].Rows[0]["PrizeID"].ToString() != "")
                {
                    model.PrizeID = int.Parse(ds.Tables[0].Rows[0]["PrizeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateTime"] != null && ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EditTime"] != null && ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["State"] != null && ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderAccount"] != null && ds.Tables[0].Rows[0]["OrderAccount"].ToString() != "")
                {
                    model.OrderAccount = ds.Tables[0].Rows[0]["OrderAccount"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserName"] != null && ds.Tables[0].Rows[0]["UserName"].ToString() != "")
                {
                    model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserQQ"] != null && ds.Tables[0].Rows[0]["UserQQ"].ToString() != "")
                {
                    model.UserQQ = ds.Tables[0].Rows[0]["UserQQ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserPhone"] != null && ds.Tables[0].Rows[0]["UserPhone"].ToString() != "")
                {
                    model.UserPhone = ds.Tables[0].Rows[0]["UserPhone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserAddr"] != null && ds.Tables[0].Rows[0]["UserAddr"].ToString() != "")
                {
                    model.UserAddr = ds.Tables[0].Rows[0]["UserAddr"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PrizeName"] != null && ds.Tables[0].Rows[0]["PrizeName"].ToString() != "")
                {
                    model.PrizeName = ds.Tables[0].Rows[0]["PrizeName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,PrizeID,CreateTime,EditTime,State,OrderAccount,UserName,UserQQ,UserPhone,UserAddr ");
            strSql.Append(" FROM PrizeRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public bool BatchEdit(string startTime, string endTime, string state1, string state2, string userAccount)
        {
            string sql = "update dbo.PrizeRecord set State =" + state2 + " where State =" + state1;
            if (!string.IsNullOrEmpty(startTime))
                sql += " and CreateTime>='" + startTime + "'";
            if (!string.IsNullOrEmpty(endTime))
                sql += " and CreateTime <='" + endTime + "'";
            if (!string.IsNullOrEmpty(userAccount))
                sql += " and UserID =(select ID from [user] where UserAccount ='" + userAccount + "')";
            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// 获得多张表的数据列表
        /// </summary>
        public DataSet GetAllList(int pageIndex, int pageSize, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from( select pr.ID,pr.CreateTime,pr.EditTime,u.UserAccount,pr.state as StateInfo,case pr.state when '1' then '审核中' when '2' then '处理中' when '3' then '已兑换' when '4' then '拒绝' else '其他' end as StateInfoName,pr.UserName,pr.UserQQ,pr.UserPhone,pr.UserAddr,u.Mac,u.IMSI,p.Name as Pname,ui.Name as Uiname,pr.OrderAccount,ROW_NUMBER() OVER(order by pr.CreateTime desc) as RowNumber ");
            strSql.Append("from PrizeRecord pr left join [User] u on pr.UserID=u.ID left join prize p on pr.PrizeID=p.ID left join UserInfo ui on u.UserInfoID=ui.ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(")a where  RowNumber BETWEEN " + ((pageIndex - 1) * pageSize + 1) + " and " + pageIndex * pageSize + ";");
            strSql.Append(" select COUNT(1) from PrizeRecord pr left join [User] u on pr.UserID=u.ID left join prize p on pr.PrizeID=p.ID left join UserInfo ui on u.UserInfoID=ui.ID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,UserID,PrizeID,CreateTime,EditTime,State,OrderAccount,UserName,UserQQ,UserPhone,UserAddr ");
            strSql.Append(" FROM PrizeRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "PrizeRecord";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

